package Analyzer;

import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.TreeSet;
import sun.reflect.generics.tree.Tree;

public class Analyzer 
{
    private String inputString;
    private static LinkedList words;
    private static LinkedList ending;
    private String forAnalysis;
    private boolean match;
    private TreeSet tree;
    
    private int size;
    
    public Analyzer(String _inputString)
    {
        inputString = _inputString;
        words = new LinkedList();
        ending = new LinkedList();
        tree = new TreeSet();
    }
    
    /**
     * Razbivaem stroku na slova i vnosil ih v list
     */
    public void Run()
    {
        StringTokenizer token = new StringTokenizer(inputString);
        
        while(token.hasMoreElements())
            words.add(token.nextToken());
    }//end of Run
    
    public void Search()
    {
        for (int i = 0; i < words.size(); i++)
        {
            forAnalysis = (String) words.get(i);
            forAnalysis = forAnalysis.toLowerCase();
            forAnalysis.length();
            
            
            if(i == 0)
            {
                for (int j = 0; j < forAnalysis.length(); j++)
                {
                    //forAnalysis.substring(j, forAnalysis.length() - 1);
                    ending.add(forAnalysis.substring(j, forAnalysis.length()));
                    ending.add(1);
                }//end of for length
            }//end of if
            else
            {
                for (int j = 0; j < forAnalysis.length(); j++)
                {
                    match = false;
                    
                    size = ending.size();
                    
                    for(int k = 0; k < size; k = k + 2)
                    {
                        String saved = (String)ending.get(k);
                        String toSave = forAnalysis.substring(j, forAnalysis.length());
                        if(saved.equalsIgnoreCase(toSave))
                        {
                            ending.set(k + 1, (int)ending.get(k+1) + 1);
                            match = true;
                        }
                    }//end of for k
                    
                    if(match == false)
                    {
                        ending.add(forAnalysis.substring(j, forAnalysis.length()));
                        ending.add(1);
                    }
                    
                }//end of for j
            }//end of else
            
        }//end of for words
    }//end of Search
    
    public LinkedList getEnding()
    {
        return ending;
    }
    
    public void Sorting()
    {
        for (int i = 3 ; i < ending.size(); i=i+2)
        {
            Integer temp = (int)ending.get(i);
            //String tempS = (String)ending.get(i - 1);
            int j = i;
            for (; j > 0 && temp.compareTo((int)ending.get(j-2)) < 0; j=j-2)
            {
                ending.set(j, ending.get(j - 2));
                //ending.set(j-1, ending.get(j - 3));
            }
            ending.set(j, temp);
            //ending.set(j - 1, tempS);
        }
    }
    
    
    /**
     * Test method
     * @param args 
     */
    public static void main(String args[])
    {
        Analyzer an = new Analyzer("LongWord Word blobs");
        an.Run();
        an.Search();
        
        /*for(int i = 0; i < words.size(); i++)
        {
            System.out.println(words.get(i));
        }*/
        
        for(int i = 0; i < ending.size(); i = i + 2)
        {
            System.out.println(ending.get(i) + " " + ending.get(i+1));
        }
    }
    
}//end of Analyzer
